<template>
	<!-- 修改调拔单 -->
	<div class="page" v-loading="loading">
		<el-breadcrumb class="breadcrumb" separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>修改调拔单</el-breadcrumb-item>
		</el-breadcrumb>
		<el-form class="content" ref="form" :model="form" status-icon :rules="rules" label-width="120px">
			<el-form-item label="类型">
				<span>调拔单</span>
			</el-form-item>
			<el-form-item label="金额" prop="amount">
				<el-input size="small" v-model="form.amount"></el-input>
			</el-form-item>
			<el-form-item label="发票编号" prop="invoiceNumber">
				<el-input size="small" v-model="form.invoiceNumber"></el-input>
			</el-form-item>
			<el-form-item label="收付款凭证号" prop="paymentNumber">
				<el-input size="small" v-model="form.paymentNumber"></el-input>
			</el-form-item>
			<el-form-item label="数量" prop="qty">
				<el-input size="small" v-model="form.qty"></el-input>
			</el-form-item>
			<select-one-supplier v-on:sendGoodsData="getSupplierData" :sendUpdateData="updateSupplierData" :sendStatus="status"></select-one-supplier>
			<select-more-goods v-on:sendSomeGoodsData="getMoreGoodsData" :sendUpdateData="form.stockUseItems" :sendStatus="status" ref="moreGoods"></select-more-goods>
			<el-form-item label="单据编号" prop="receiptNumber">
				<el-input size="small" v-model="form.receiptNumber"></el-input>
			</el-form-item>
			<el-form-item label="单位" prop="unit">
				<el-input size="small" v-model="form.unit"></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input size="small" v-model="form.remark"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button size="small" type="primary" @click="submitForm('form')">确认修改</el-button>
				<el-button size="small" @click="utils.back">取消</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>
<script>
import axios from 'axios';
import selectOneSupplier from '../../common/selectOneSupplier.vue';
import selectMoreGoods from '../../common/selectMoreGoods.vue';
export default {
	name: "add",
	data() {
		return {
			status :0,
			form: {
				"amount": 0,				//总金额 单位分
				"invoiceNumber": "",		//发票编号
				"paymentNumber": "",		//收付款凭证号
				"qty": 0,					//总数量
				"receiptNumber": "",		//单据编号
				"stockUseType": "REQUISITION",	//单据类型
				"supplierContact": "",		//供应商联系人
				"supplierId": '',			//供应商ID
				"supplierMobile": "",		//手机号
				"supplierName": "",			//供应商
				"unit": "",					//单位 （瓶）
				"remark": "",				//备注
				"stockUseItems": []
			},
			hasSupplierData: {

			},
			rules: {
				amount: [{ required: true, message: '金额不能为空', trigger: 'blur' }],
				invoiceNumber: [{ required: true, message: '发票编号不能为空', trigger: 'blur' }],
				paymentNumber: [{ required: true, message: '收付款凭证号不能为空', trigger: 'blur' }],
				qty: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
				receiptNumber: [{ required: true, message: '单据编号不能为空', trigger: 'blur' }],
				stockUseType: [{ required: true, message: '类型不能为空', trigger: 'blur' }],
				unit: [{ required: true, message: '单位不能为空', trigger: 'blur' }],
				remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
			},
			loading: true,
			city: '',
			updateSupplierData: [],
		}
	},
	components: {
		selectOneSupplier,
		selectMoreGoods
	},
	created() {
		this.status = 1;
		this.getDetails();
	},
	mounted() {
	},
	methods: {
		getDetails() {
			this.loading = true;
			this.$post("stockUseDetails", {
				id: this.$route.params.id
			}).then(res => {
				this.form = res.data;
				var item = {
					id: this.form.supplierId,
					supplierName: this.form.supplierName,
					supplierMobile: this.form.supplierMobile,
					supplierContact: this.form.supplierContact,
				}
				this.updateSupplierData.push(item);
				console.log(this.updateSupplierData)
				this.loading = false;
			}).catch((e)=>{
				this.loading = false;
			})
		},
		// 获取子组件（选择商品）数据
		getMoreGoodsData(data){
			console.log(data,this.form.stockUseItems)
			for(var i in data){
				data[i].goodsId = data[i].id;
				this.form.stockUseItems.push(data[i]);
			}
		},
		// 获取子组件（选择供应商）数据
		getSupplierData(dataID, data) {
			console.log(dataID, data)
			this.supplierContact = data;
			this.supplierId = dataID;
			this.supplierMobile = data.supplierChannelNo;		//供应商手机
			this.supplierName = data.supplierName;
		},
		// 添加进货单
		submitForm(formName) {
			this.loading = true;
			this.$refs[formName].validate((valid) => {
				if (valid) {
					this.$post("stockUseUpdate", this.form).then((res) => {
						this.loading = false;
						if (res.code == 200) {
							this.$msg.success("修改成功～");
							this.$router.back();
						} else {
							this.$msg.warning(res.message);
						}
					})
				} else {
					this.$msg.error("网络或服务器错误");
					this.loading = false;
				}
			});
		},
	}
};
</script>
<style lang="less" scoped>
.page {
  .content {
    width: 1000px;
    margin: 0 auto;
    .el-input {
      width: 600px;
    }
    .add-data {
      .el-input {
        width: 80%;
      }
    }
  }
}
</style>